package com.atguigu.scala.chapter6

import java.io.{FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}
import scala.beans.BeanProperty

/**
 * @author lizikuan
 * @date 2021/5/22 11:00
 */
object $06_SelfProperty {
  trait Pbook{
    this:Serializable=>
    def read() = {
      val ois = new ObjectInputStream(new FileInputStream("C:\\Users\\34640\\Desktop\\data1.txt"))
      val value = ois.readObject()
      ois.close()
      value
    }

    def write() = {
      val oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\34640\\Desktop\\data1.txt"))
      oos.writeObject(this)
      oos.flush()
      oos.close()
    }
  }


    class Book extends Pbook with Serializable {
     def this(name:String,age:Int,id:Int){
       this()
       this.setId(id)
       this.setAge(age)
       this.setName(name)
     }
      @BeanProperty
    var name:String = _
    @BeanProperty
    var age:Int = _
    @BeanProperty
    var id:Int = _
  }

  def main(args: Array[String]): Unit = {
    val book = new Book("zhangsan",20,3)
    println(book.getAge)
    book.write()
    val book2 = new Book
    val value = book2.read()
    val book1 = value.asInstanceOf[Book]
    println(book1.getName)
  }
}
